package com.sg.business.model.dataset.work;

import java.util.ArrayList;
import java.util.List;

import com.mobnut.db.model.DataSet;
import com.mobnut.db.model.PrimaryObject;
import com.mobnut.db.model.mongodb.SingleDBCollectionDataSetFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sg.business.model.ILifecycle;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.Project;
import com.sg.business.model.Work;

public class AllProcessingWork extends SingleDBCollectionDataSetFactory {

	public AllProcessingWork() {
		super(IModelConstants.DB, IModelConstants.C_WORK);
	}

	@Override
	public DBObject getQueryCondition() {
		DBObject condition = super.getQueryCondition();
		if (condition == null) {
			condition = new BasicDBObject();
		}
		condition.put(Work.F_LIFECYCLE, new BasicDBObject().append("$in",
				new String[] { ILifecycle.STATUS_WIP_VALUE }));

		condition.put(Work.F_WF_EXECUTE_ACTIVATED, Boolean.TRUE);

		return condition;
	}

	@Override
	public DataSet getDataSet() {
		DataSet ds = super.getDataSet();
		List<PrimaryObject> items = ds.getDataItems();
		List<PrimaryObject> result = new ArrayList<PrimaryObject>();
		for (int i = 0; i < items.size(); i++) {
			Work item = (Work) items.get(i);
			result.add(createItem(item));
		}
		return new DataSet(result);
	}

	private PrimaryObject createItem(Work item) {
		Project project = item.getProject();
		if (project != null) {
			item.setValue("projectdesc", project.getDesc());
		}

		return item;
	}
}
